package com.mustafaiev.tair.autobase.web.command;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.Calendar;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.mustafaiev.tair.autobase.dao.WorkerDao;
import com.mustafaiev.tair.autobase.dao.WorkerDaoImpl;
import com.mustafaiev.tair.autobase.db.DBConnector;
import com.mustafaiev.tair.autobase.web.AutobaseServlet;
import com.mustafaiev.tair.autobase.worker.Worker;

/**
 * 
 * The Command class implements {@link AutobaseServletCommand} and provides
 * worker registration
 * 
 * @author Tair Mustafaiev
 * 
 */
public class RegisterCommandImpl implements AutobaseServletCommand {

    private static final Logger LOG = Logger.getLogger(RegisterCommandImpl.class.getName());

    @Override
    public void processGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

        // forwards request to register page
        req.getSession().getServletContext().getRequestDispatcher("/registration.jsp")
                .forward(req, resp);
    }

    @Override
    public void processPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        
        Worker worker = new Worker();
        DBConnector connector = DBConnector.getInstance();
        WorkerDao dao = new WorkerDaoImpl();
        dao.setConnector(connector);
        
        try {
            
            
            if (req.getParameter("name") != null) {
                worker.setName(req.getParameter("name"));
            }
            if (req.getParameter("surname") != null) {
                worker.setSurname(req.getParameter("surname"));
            }
            if (req.getParameter("pass") != null) {
                worker.setPassword(req.getParameter("pass"));
            }
            if (req.getParameter("email") != null) {
                worker.setEmail(req.getParameter("email"));
            }
            
            if (!dao.checkWorkerByEmail(worker)) {
                dao.addNewWorker(worker);
                LOG.info("Added new worker: " + "name: " + worker.getName() + " surname: "
                        + worker.getSurname() + " email: " + worker.getEmail() + " date: "
                        + Calendar.getInstance().getTime());

                req.getSession(true).setAttribute("message",
                        "You are successful registered, Thank You!");
                resp.sendRedirect("/index.jsp");
            } else {
                req.getSession(true).setAttribute("error",
                        "Worker with same email already exists, please valid your email...");
                resp.sendRedirect("/register.do");
            }

        } catch (SQLException e) {
            LOG.error("Database error when user registration was in process: " + e);
            req.getSession(true).setAttribute("error",
                    "Worker with same email already exists, please valid your email...");
            resp.sendRedirect("/register.do");
        }

    }

}
